package cn.swu.lzr.JDBC;

import java.security.PublicKey;
import java.sql.*;

public class DBUtil {
    private static  final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static  final String URL = "jdbc:mysql://127.0.0.1:3306/events";
    private static final String USERNAME = "root";
    private static  final  String password = "1911lzrnb";

    public static Connection getConnection()
    {
        Connection connection = null;
        try {
            Class.forName(DRIVER);
            connection =  DriverManager.getConnection(URL,USERNAME,password);

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return connection;
    }

    public static void closeConnection(Connection connection)
    {
        if (connection != null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public static void CloseStatement(Statement statement) throws SQLException
    {
        if(statement != null)
        {
            try {
                statement.close();
            }catch (SQLException e)
            {
                throw new RuntimeException();
            }
        }
    }

    public static void  ClosePrepareStatement(PreparedStatement preparedStatement)throws  SQLException
    {

            if (preparedStatement != null)
            {
                try {
                    preparedStatement.close();
                }catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
    }
    public static void CloseResultSet(ResultSet resultSet)
    {
        if (resultSet != null)
        {
            try{
                resultSet.close();;
            }catch (Exception e)
            {
                e.printStackTrace();;
            }
        }
    }



}
